(function(app){
	app
		.controller("content_04_inputCtrl",["$scope","$state","myFactory",'$stateParams',"$q",'$timeout','$location','$http','$ionicHistory',function($scope,$state,myFactory,$stateParams,$q,$timeout,$location,$http,$ionicHistory){
			//参数定义、接收
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var policyData;//保单详情
			var itemData;//用于接收初始化查询initContentItem的内容，里面存在各种各样的内容，比如item02_1
			var searchParam = $stateParams.searchParam;
			var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
			var token=sessionStorage.getItem("token");
			//绑定参数到页面
			$scope.chdrnum01 = chdrnum01;
			$scope.declarationFlag = false;//声明标志，默认为false
			$scope.readFlag = false;//阅读协议标志，默认为false

			$scope.data = {
				checkFlag: false,
				wayVal: null,
				wayList: [
				],
			};

			/**********************************************初始化函数begin****************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						$scope.policy = response.data.data.policy;
						itemData = response.data.data.modelMap;
						var item04 = itemData.item04;

						/*$("#billFreq").val("00");
						$scope.occupyCheckInit();
						var options = $("#billFreq option:selected");
						console.log("id:"+options.val());*/
						$scope.billFreq="00";

						var b=$scope.policy.base.billfreqdsc01;
						//var a="${billFreq}";
						if(b=="年缴"){
							$("#billFreq option[value='01']").remove();
						}
						if(b=="半年缴"){
							$("#billFreq option[value='02']").remove();
						}
						if(b=="季缴"){
							$("#billFreq option[value='04']").remove();
						}
						if(b=="月缴"){
							$("#billFreq option[value='12']").remove();
						}

						initFlag=true;//标识初始化成功
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
			/**********************************************初始化函数end****************************************************/

			//下拉框
			$scope.occupyCheck = function(){
				var options = $("#billFreq option:selected");
				console.log("id:"+options.val());
				console.log("val:"+options.text());
				$scope.reasonInput=options.text();
				$scope.billFreq=options.val();
				$("#billFreq").val(options.val());
				var billFreq = $("#billFreq").val();
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-billFreq-token-timestamp";
				var signStr="itemId="+itemId+"&billFreq="+billFreq+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/saveContentItem04NewCost.action',
					params: {'itemId': itemId,'billFreq': billFreq,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						var item04NewCost = response.data.data.item04NewCost;
						$scope.newprem=item04NewCost.newprem;//变更后期缴保费金额
						$scope.actualamount=item04NewCost.actualamount;//本次变更应缴保险费金额
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","查询数据失败!","info");
				});
			}

			/*$scope.occupyCheckInit = function(){
				var options = $("#billFreq option:selected");
				$scope.reasonInput = options.text();
				console.log("idTemp:"+options.val());
				//$("#billFreq").val(options.val());
			}*/

			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			//返回上一页
			$scope.goBack = function(){
				myFactory.goBack();
			}
			//关闭遮罩
			$scope.overwindowClose = function(){
				//$(".overwindow").show();
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
			//保单详情弹框
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn04Input','overwindowOn04Input','policyCloseXOn04Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}
            //保单详情弹窗关闭函数
			$scope.policyCancelOn04Input = function(){
				modal('close','policyModalOn04Input','overwindowOn04Input','policyCloseXOn04Input');
			}

			//客户须知
			$scope.tipOpen = function(){
				modal('open','tip_model','overwindowOn04Input');
			}
			$scope.closeTip = function(){
				modal('close','tip_model','overwindowOn04Input');
			}
			//缴费方式选择弹框
			/*$scope.wayChose = function(){
				modal('open','way_model','overwindow');
			}
			$scope.wayClose = function(){
				modal('close','way_model','overwindow');
			}
			$scope.wayClick = function (val) {
				$scope.data.wayVal = val;
			}*/
            //页面同意阅读协议复选框部分
			$scope.changeReadFlag = function(){
				$scope.readFlag = !$scope.readFlag;
				console.log($scope.readFlag);
			}
			
			//下一步
			$scope.nextStep = function(){
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}

				//var options = $("#billFreq option:selected");
				//var billFreq=$("#billFreq").val();
				//console.log("id:"+options.val());
				var billFreq=$scope.billFreq;
				console.log("billFreq:"+billFreq);

				//var options = $("#billFreq option:selected");
				//var billFreq = options.val(); //选择缴费方式
				//console.log(billFreq);
				//var billFreq = $("#billFreq").val();
				if(billFreq=="00"){
					myFactory.alert("","请选择缴费方式","info");
					return;
				}
				var readFlag = $scope.readFlag;
				if(readFlag==false){
					myFactory.alert("","请同意并阅读《申请须知》","info");
					return;
				}

				var item={
					"itemId":itemId,
					"billFreq":billFreq
				}

				$ionicHistory.clearCache().then(function(){
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy,
						searchParam:searchParam
					});
				})
			}
		}]);

		//指针弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});

})(app)